package com.javasm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.IndentCriteria;
import com.javasm.domin.entity.Indent;

import com.javasm.domin.vo.IndentGoodsVo;
import com.javasm.domin.vo.IndentVo;
import com.javasm.mapper.IndentMapper;
import com.javasm.service.IndentService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.IndentTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author longjun
 * @since 2022-01-16
 */
@Service
@RequiredArgsConstructor
@Transactional//开启事务第二步
public class IndentServiceImpl extends BaseServiceimpl<Indent> implements IndentService {

    private final IndentMapper indentMapper;
    private final IndentTransfer indentTransfer;

    @Override
    public PageResult<IndentVo> searchPage(IndentCriteria indentCriteria) {
        //开启分页
        PageHelper.startPage(indentCriteria.getCurrentPage(), indentCriteria.getPageSize());
        //条件创建
        LambdaQueryWrapper<Indent> lambda = new QueryWrapper<Indent>().lambda();
        //判断品牌名称
        if (!StringUtils.isEmpty(indentCriteria.getId())) {
            lambda.like(Indent::getId,indentCriteria.getId());
        }
        //判断订单类型
        if(!StringUtils.isEmpty(indentCriteria.getIndentType())){
            lambda.like(Indent::getIndentType,indentCriteria.getIndentType());
        }
        //判断订单动作
        if(!StringUtils.isEmpty(indentCriteria.getIndentAction())){
            lambda.like(Indent::getIndentAction,indentCriteria.getIndentAction());
        }
        //判断订单状态
        if(!StringUtils.isEmpty(indentCriteria.getIndentState())){
            lambda.like(Indent::getIndentState,indentCriteria.getIndentState());
        }
        //判断支付方式
        if(!StringUtils.isEmpty(indentCriteria.getPayType())){
            lambda.like(Indent::getPayType,indentCriteria.getPayType());
        }
        //判断开始时间以及结束时间是否为空
        LocalDateTime startTime = indentCriteria.getStartTime();
        LocalDateTime endTime = indentCriteria.getEndTime();
        if (startTime != null && endTime != null) {
            //根据between进行判断
            lambda.between(Indent::getCreateTime, startTime, endTime);
        }
        List<Indent> indentList = indentMapper.selectList(lambda);
//        System.out.println("indentList===="+indentList);
        //获取总条数
        PageInfo<Indent> pageInfo = new PageInfo<>(indentList);
        //返回总条数以及转换成vo对象

        return new PageResult<IndentVo>(pageInfo.getTotal(),indentTransfer.toVO(indentList));
    }

    @Override
    public List<IndentVo> searchIndentList() {
        //查询所有的条数
        List<Indent> indentList = indentMapper.selectList(null);
        //转成vo
        List<IndentVo> indentVoList = indentTransfer.toVO(indentList);
        return indentVoList;
    }

    @Override
    public int deleteIndentById(Integer id) {
        return indentMapper.deleteById(id);
    }

    @Override
    public int deleteIndentByIds(List<Long> ids) {
        return indentMapper.deleteBatchIds(ids);
    }

    @Override
    public IndentVo searchIndentById(Integer id) {
        Indent indent = indentMapper.selectById(id);
        IndentVo indentVo = indentTransfer.toVO(indent);
        return indentVo;
    }

    @Override
    public IndentGoodsVo searchCountGoods(Integer id) {

        return null;
    }
}
